package com.wisdomparking.control.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.wisdomparking.bean.User;
import com.wisdomparking.config.result.TempResult;
import com.wisdomparking.control.BaseController;
import com.wisdomparking.service.ManagerService;
import com.wisdomparking.service.UserService;
import com.wisdomparking.tools.PageForAjax;
import com.wisdomparking.tools.ToolValid;

/**
 * 后台管理 - 用户中心
 */
@RestController
@RequestMapping("/web/userCenter")
public class UsersController extends BaseController{
	
	@Autowired ManagerService managerService;
	@Autowired UserService userService;
	
	/**
     * 首页
     *
     * @return
     */
    @RequestMapping(value = "/index")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
    	
        return new ModelAndView("user/index");
    }
	
	/**
     * 左侧
     *
     * @return
     */
    @RequestMapping(value = "/left")
    public ModelAndView left(HttpServletRequest request, HttpServletResponse response) {
        return new ModelAndView("user/left");
    }
    
    /**
     * 转到用户信息处理
     *
     * @return
     */
    @RequestMapping(value = "/handlePage")
    public ModelAndView handlePage() {
    	
    	if(ToolValid.isNotEmpty(request.getParameter("id"))){
    		User user = userService.getUser(Integer.parseInt(request.getParameter("id")));
    		
    		request.setAttribute("user", user);
    	}
    	
        return new ModelAndView("user/op");
    }
    
    
    /**
	 * 获取用户信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/getUser")
	public ModelAndView getManager(Integer id){
		User user = userService.getUser(id);
		request.setAttribute("user", user);
		
		return new ModelAndView("user/op");
	}
	
	
	/**
	 * 删除用户
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delUser")
	public TempResult delUser(Integer id){
		boolean flag = false;
		User user = userService.getUser(id);
		user.setDelFlag(true);
		flag = userService.updateUser(user, false);
		
		return new TempResult(flag, "");
	}
	
	
	/**
	 * 获取管理员列表
	 * @return
	 */
	@RequestMapping("/getUserList")
	public ModelAndView getManagerList(){
		
		String url = request.getContextPath() + "/web/userCenter/getUserList";
		String query = "";
		PageForAjax pagecontent = null;
				
		int pageNo = 1;
		int pageSize = 10;
		int totalcount = 0;
		
		if(null!=request.getParameter("pageNo")){
			pageNo = Integer.parseInt(request.getParameter("pageNo"));
		}
		
		if(null!=request.getParameter("pageSize")){
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		
		Map<String, Object> map = new HashMap<>();
		map.put("start", (pageNo-1)*pageSize);
		map.put("limit", pageSize);
		map.put("delFlag", 0);
		
		if(ToolValid.isNotEmpty(request.getParameter("key"))){
			map.put("mobileNumber", request.getParameter("key"));
			request.setAttribute("key", request.getParameter("key"));
		}
		
		List<User> list = userService.getUserList(map);
		totalcount = new Long(userService.getCountByUser(map)).intValue();

		pagecontent = new PageForAjax(pageNo, pageSize, totalcount, url, query);

		request.setAttribute("userList", list);
		request.setAttribute("nowpage", pagecontent);
		
		return new ModelAndView("user/users");
	}
	
	
}
